package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.StudentPageDto;
import com.example.entity.Admin;
import com.example.entity.Student;
import com.example.service.AdminService;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin //跨域问题
@RestController
@RequestMapping("/admin")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private AdminService adminService;
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        Admin loginAdmin = adminService.login(admin);
        return Result.success(loginAdmin);
    }

    @GetMapping
    public Result getStudent() {
        log.info("student的get请求");
        List<Student> list = studentService.getStudent();
        return Result.success(list);
    }

    /**
     * 根据sno和sname查询
     *
     * @return
     */
    @GetMapping("/student/search")
    public Result<PageResult> getBySnameWithSno(StudentPageDto studentPageDto) {
        log.info("根据sno和sname查询...{}", studentPageDto);
        PageResult pageResult = studentService.getBySnameWithSno(studentPageDto);
        return Result.success(pageResult);
    }

    /**
     * 新增学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/student")
    public Result<String> add(@RequestBody Student student) {
        studentService.add(student);
        return Result.success();
    }
    @PutMapping("/student")
    public Result<String> update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }

    /**
     * 根据学号删除学生信息
     *
     * @param sno
     * @return
     */
    @DeleteMapping("/student/{sno}")
    public Result<String> deleteBySno(@PathVariable String sno) {
        studentService.deleteBySno(sno);
        log.info("根据学号删除学生信息：{}", sno);
        return Result.success();
    }
}
